
package com.shop.cloud.mall.admin.service.impl;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.shop.cloud.common.core.constant.CacheConstants;
import com.shop.cloud.mall.admin.mapper.ActiveBorderMapper;
import com.shop.cloud.mall.admin.service.ActiveBorderRelationService;
import com.shop.cloud.mall.admin.service.ActiveBorderService;
import com.shop.cloud.mall.common.constant.MallConstants;
import com.shop.cloud.mall.common.entity.ActiveBorder;
import com.shop.cloud.mall.common.entity.ActiveBorderRelation;
import lombok.AllArgsConstructor;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.io.Serializable;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 活动边框
 *
 * @author
 * @date 2023-03-22 17:16:20
 */
@Service
@AllArgsConstructor
public class ActiveBorderServiceImpl extends ServiceImpl<ActiveBorderMapper, ActiveBorder> implements ActiveBorderService {

	private final ActiveBorderRelationService activeBorderRelationService;

	@Override
	@Transactional(rollbackFor = Exception.class)
	@CacheEvict(value = CacheConstants.MALL_ENABLE_ACTIVE_BORDER, allEntries = true)
	public boolean save(ActiveBorder entity) {
		super.save(entity);
		if(StrUtil.isNotBlank(entity.getSuitType()) &&
				!MallConstants.ACTIVE_SUIT_TYPE_1.equals(entity.getSuitType())){//是否指定关联
			doRelation(entity);
		}
		return Boolean.TRUE;
	}

	void doRelation(ActiveBorder entity){
		List<ActiveBorderRelation> listActiveBorderRelation = entity.getListActiveBorderRelation().stream()
				.map(ActiveBorderRelation -> {
					ActiveBorderRelation.setActiveBorderId(entity.getId());
					return ActiveBorderRelation;
				}).collect(Collectors.toList());
		activeBorderRelationService.saveBatch(listActiveBorderRelation);
	}

	@Override
	@Transactional(rollbackFor = Exception.class)
	@CacheEvict(value = CacheConstants.MALL_ENABLE_ACTIVE_BORDER, allEntries = true)
	public boolean updateById1(ActiveBorder entity) {
		super.updateById(entity);
		if(entity.getListActiveBorderRelation() != null || MallConstants.ACTIVE_SUIT_TYPE_1.equals(entity.getSuitType())){
			//先删除关联
			ActiveBorderRelation activeBorderRelation = new ActiveBorderRelation();
			activeBorderRelation.setActiveBorderId(entity.getId());
			activeBorderRelationService.remove(Wrappers.query(activeBorderRelation));
			if(StrUtil.isNotBlank(entity.getSuitType()) &&
					!MallConstants.ACTIVE_SUIT_TYPE_1.equals(entity.getSuitType())){//是否指定关联
				doRelation(entity);
			}
		}
		return Boolean.TRUE;
	}

	@Override
	public ActiveBorder getById2(Serializable id) {
		return baseMapper.selectById2(id);
	}

	@Override
	@Transactional(rollbackFor = Exception.class)
	@CacheEvict(value = CacheConstants.MALL_ENABLE_ACTIVE_BORDER, allEntries = true)
	public boolean removeById(Serializable id) {
		//先删除关联
		ActiveBorderRelation activeBorderRelation = new ActiveBorderRelation();
		activeBorderRelation.setActiveBorderId(id.toString());
		activeBorderRelationService.remove(Wrappers.query(activeBorderRelation));
		return super.removeById(id);
	}
}
